package com.andoop.scoredemo;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.andoop.highscore.api.HighScoreManager;

public class Main extends Activity {
	private HighScoreManager _scoreManager;
	private Random _rand = new Random();
	private TextView _scoreTv = null;
	private Button _subboardBtn = null;
	private int _score = 0;
	private String _scoreDisplay = "";
	private String _subboard = "";
	private String[] _subboards = new String[] {"Level-1", "Level-2", "Level-3"};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		_scoreManager = new HighScoreManager(this);
		_scoreTv = (TextView) findViewById(R.id.score);
		_subboardBtn = (Button) findViewById(R.id.submit_subboard_score);
		initScore();
		
		Button btn = (Button) findViewById(R.id.submit_score);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				_scoreManager.submitScore(_score);
			}
		});
		
		_subboardBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				_scoreManager.submitScore(_score, _scoreDisplay, _subboard);
			}
		});
		
		btn = (Button) findViewById(R.id.retry);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				initScore();
			}
		});
	}
	
	protected void initScore() {
		_score = _rand.nextInt(1000);
		_scoreDisplay = "Lucky Score " + _score;
		_subboard = _subboards[_rand.nextInt(3)];
		_scoreTv.setText(_scoreDisplay);
		
		String format = getString(R.string.submit_subboard_score);
		_subboardBtn.setText(String.format(format, _subboard));
	}
}